from django.db import models


# Create your models here.
from polls.util import make_sha256_digest


class User(models.Model):
    userid = models.AutoField(primary_key=True,verbose_name='编号')
    username = models.CharField(max_length=20,unique=True,verbose_name='用户名')
    password = models.CharField(max_length=64,verbose_name='密码')
    tel =models.CharField(max_length=11,unique=True,verbose_name='电话号码')
    reg_date = models.DateField(auto_now_add=True,verbose_name='注册时间')
    last_visit = models.DateField(null=True,verbose_name='最后登录时间')

    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
        if self.pk:
            self.password = make_sha256_digest(self.password)
        super().save(force_insert=False, force_update=False, using=None,
             update_fields=None)

    class Meta:
        db_table = 'tb_user'
        verbose_name = '用户'
        verbose_name_plural = '用户'



class Subject(models.Model):
    no = models.AutoField(primary_key=True, verbose_name='编号')
    name = models.CharField(max_length=50, verbose_name='名称')
    infro = models.CharField(max_length=2000, verbose_name='介绍')
    is_hot = models.BooleanField(default=False, verbose_name='是否热门')
    create_date = models.CharField(max_length=50, default='战国末年', verbose_name='成立时间')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'tb_subject'
        verbose_name = '门派'
        verbose_name_plural = '门派'


class Teacher(models.Model):
    no = models.AutoField(primary_key=True, verbose_name='编号')
    name = models.CharField(max_length=50, verbose_name='姓名')
    gender = models.BooleanField(default=True, choices=((1,'男'),(0,'女')),verbose_name='性别')
    hero = models.CharField(max_length=255, verbose_name='身份')
    intro = models.CharField(max_length=2000, verbose_name='简介')
    photo = models.CharField(max_length=512, verbose_name='头像')
    good_count = models.IntegerField(default=0, verbose_name='好评')
    bad_count = models.IntegerField(default=0, verbose_name='差评')

    subject = models.ForeignKey(to=Subject, on_delete=models.PROTECT, db_column='sno', verbose_name='门派')

    class Meta:
        db_table = 'tb_teacher'
        verbose_name = '人物'
        verbose_name_plural = '人物'
